package com.onetech.adrewards.free.dialogs;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.MapBuilder;
import com.google.analytics.tracking.android.Tracker;
import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.activities.BaseNewActivity;
import com.onetech.adrewards.free.config.AppConfig;
import com.onetech.adrewards.free.http.APIRequestResult;
import com.onetech.adrewards.free.models.CommonModel;
import com.onetech.adrewards.free.utils.AdrewadsUtils;
import com.todddavies.components.progressbar.ProgressWheel;
import com.zuzu.dialogs.BaseDialogNoTitleBar;

public class LoginBonusDialog extends BaseDialogNoTitleBar {
	private ImageView mItemBonus1 = null;
	private ImageView mItemBonus2 = null;
	private ImageView mItemBonus3 = null;
	private ProgressWheel mItemBonusProgressBar1=null;
	private ProgressWheel mItemBonusProgressBar2=null;
	private ProgressWheel mItemBonusProgressBar3=null;
	private TextView mBonusTxt1=null;
	private TextView mBonusTxt2=null;
	private TextView mBonusTxt3=null;
	
	private TextView mSuccessMessage=null;
	
	
	
	private boolean isClicked=false;
	private View mCloseBtn=null;
	
	private RandomBonusFromServer mRandomBonusFromServer=null;
	private Activity activity=null;
	private Tracker mTracker;
	private Context mContext=null;
	
	public LoginBonusDialog(Activity _activity) {
		super(_activity, _activity.getString(R.string.dialog_login_bonus_title),
				TYPE_NO_BUTTON, R.layout.dialog_login_bonus_layout);
		activity=_activity;
		
		initView();
		initListener();
	}

	private void initView() {
		mItemBonus1 = (ImageView) findViewById(R.id.itemBonus1);
		
		mItemBonus2 = (ImageView) findViewById(R.id.itemBonus2);
		mItemBonus3 = (ImageView) findViewById(R.id.itemBonus3);
		
		mItemBonusProgressBar1=(ProgressWheel)findViewById(R.id.itemBonusProgressBar1);		
		mItemBonusProgressBar2=(ProgressWheel)findViewById(R.id.itemBonusProgressBar2);
		mItemBonusProgressBar3=(ProgressWheel)findViewById(R.id.itemBonusProgressBar3);
		mItemBonusProgressBar1.spin();
		mItemBonusProgressBar2.spin();
		mItemBonusProgressBar3.spin();
		
		mBonusTxt1=(TextView)findViewById(R.id.itemBonusTxt1);
		mBonusTxt2=(TextView)findViewById(R.id.itemBonusTxt2);
		mBonusTxt3=(TextView)findViewById(R.id.itemBonusTxt3);
		
		
		
		mSuccessMessage=(TextView)findViewById(R.id.success_bonus_message);
		String msg = activity.getString(R.string.login_bonus_card_title); 
		msg = msg.replace("[daily_bonus]", String.valueOf(AppConfig.BONUS_MAX_COIN));
		mSuccessMessage.setText(Html.fromHtml(msg));
		
		mCloseBtn=(LinearLayout)findViewById(R.id.closeBtn);
		
	}

	private void initListener() {
		mItemBonus1.setOnClickListener(this);
		mItemBonus2.setOnClickListener(this);
		mItemBonus3.setOnClickListener(this);
		mCloseBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		int id=v.getId();
		if(isClicked){
			if(id==R.id.itemBonus1 || id==R.id.itemBonus2|| id==R.id.itemBonus3){
				Toast.makeText(getContext(), getContext().getString(R.string.dialog_login_bonus_already_done), Toast.LENGTH_LONG).show();
			}
			super.onClick(v);
		}else{
			switch (id) {
			case R.id.itemBonus1:
				randomBonus(1);
				break;
			case R.id.itemBonus2:
				randomBonus(2);
				break;
			case R.id.itemBonus3:
				randomBonus(3);
				break;
			default:
				super.onClick(v);
				break;
			}
		}
		if(id==R.id.closeBtn){
			dismiss();
		}
	}
	private void randomBonus(int _curItem){
		if(mRandomBonusFromServer==null || mRandomBonusFromServer.getStatus()==Status.FINISHED){
			mRandomBonusFromServer=new RandomBonusFromServer(_curItem);
			mRandomBonusFromServer.execute();
		}
	}
	private class RandomBonusFromServer extends AsyncTask<Void, Void, APIRequestResult>{
		private int currentItem=0;
		public RandomBonusFromServer(int _curItem){
			currentItem=_curItem;
			isClicked=true;
		}
		@Override
		protected void onPreExecute() {
			switchProgressBarVisible(currentItem, true);
			super.onPreExecute();
		}
		@Override
		protected APIRequestResult doInBackground(Void... params) {
			return CommonModel.getInstance().getBonusLogin();
		}
		@Override
		protected void onPostExecute(APIRequestResult result) {
			super.onPostExecute(result);
			if(result!=null){
				try {
					if (result.error_code.equals("0") && result.data != null && !result.data.equals("")) {
						JSONObject jsonObject=(JSONObject)result.data;
						int bonusCoins = jsonObject.getJSONObject("result").getInt("coin_bonus");
						//Toast.makeText(getContext(), "Bonus Coins:"+bonusCoins, Toast.LENGTH_LONG).show();
						switchBonusBackground(currentItem,bonusCoins);
						AppConfig.LOGIN_BONUS=false;
						String message= getContext().getString(R.string.login_bonus_success_message);
						message = message.replace("[coin]", String.valueOf(bonusCoins));
						mSuccessMessage.setText(Html.fromHtml(message));
						((BaseNewActivity)activity).getUserInfo();
					}else{
						AdrewadsUtils.showMessage(getContext(), result.error_msg);
						AppConfig.LOGIN_BONUS=false;
					}
				} catch (Exception e) {
					AppConfig.LOGIN_BONUS=false;
				}		
			}else{
				AdrewadsUtils.showMessage(getContext(), getContext().getString(R.string.login_bonus_failed_error_message));				
			}
			switchProgressBarVisible(currentItem, false);	
			//switchBonusBackground(currentItem,5);//just for test
		}
	}
	private void switchProgressBarVisible(int currentItem,boolean isShow){
		switch (currentItem) {
		case 1:
			mItemBonusProgressBar1.setVisibility((isShow==true)?View.VISIBLE:View.GONE);
			break;
		case 2:
			mItemBonusProgressBar2.setVisibility((isShow==true)?View.VISIBLE:View.GONE);
			break;
		case 3:
			mItemBonusProgressBar3.setVisibility((isShow==true)?View.VISIBLE:View.GONE);
			break;
		default:
			break;
		}
	}
	private void switchBonusBackground(int currentItem,int bonusCoins){
		switch (currentItem) {
		case 1:
			mItemBonus1.setBackgroundResource(R.drawable.ic_card_random_selected);
			mBonusTxt1.setText(String.valueOf(bonusCoins));
			break;
		case 2:
			mItemBonus2.setBackgroundResource(R.drawable.ic_card_random_selected);
			mBonusTxt2.setText(String.valueOf(bonusCoins));
			break;
		case 3:
			mItemBonus3.setBackgroundResource(R.drawable.ic_card_random_selected);
			mBonusTxt3.setText(String.valueOf(bonusCoins));
			break;
		default:
			break;
		}
	}
	@Override
    public void onStart() {
        super.onStart();
        try {
            mTracker.set(Fields.SCREEN_NAME, activity.getString(R.string.LoginBonusDialog));            
            mTracker.send(MapBuilder.createAppView().build());            
		} catch (Exception e) {
		}
    }
}
